package com.syh.jn.scm.infrastructure.repository.impl.operation.system;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoMybatisImplTmpl;
import com.syh.jn.scm.domain.operation.repository.system.OperationMenuReadRepository;
import com.syh.jn.scm.dto.operation.system.menu.OperaMenuVo;
import com.syh.jn.scm.infrastructure.repository.model.system.MenuModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("operationMenuReadRepositoryMbImpl")
public class OperationMenuReadRepositoryMbImpl implements OperationMenuReadRepository {


  private AggregateRepoMybatisImplTmpl<OperaMenuVo, MenuModel, Long> mbImplTmpl = new AggregateRepoMybatisImplTmpl.Builder()
          .listTypeClass(OperaMenuVo.class)
          .modelClass(MenuModel.class).build();


  public OperationMenuReadRepositoryMbImpl() {
    mbImplTmpl.setQuerySql(this.getViewSql());
    mbImplTmpl.setWrapperParseMethods((propertyName -> {
      return this.getViewWrapperMap().get(propertyName);
    }));
  }

  @Override
  public OperaMenuVo getById(Long aLong) {
    return mbImplTmpl.getById(aLong);
  }

  @Override
  public OperaMenuVo getOne(Wrapper wrapper) {
    return mbImplTmpl.getOne(wrapper);
  }

  @Override
  public List<OperaMenuVo> getList(Wrapper wrapper) {
    return mbImplTmpl.getList(wrapper);
  }

  @Override
  public boolean contains(Long aLong) {
    return mbImplTmpl.contains(aLong);
  }

  @Override
  public List<OperaMenuVo> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
    return mbImplTmpl.findListPage(wrapper, startRow, count);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    return mbImplTmpl.getCount(wrapper);
  }

  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(OperaMenuVo.class, "menu1", null, new String[]{"superName"}),
              "menu2.NAME as superName"
      );
      FROM("T_SYSTEM_MENU menu1");
      LEFT_OUTER_JOIN("T_SYSTEM_MENU menu2 ON menu1.SUPER_ID = menu2.ID ");
    }};
  }

  protected Map<String, String> getViewWrapperMap() {
    Map<String, String> map = new HashMap<>();
    map.put("catalogCode", "menu1.CATALOG_CODE");
    map.put("terminalType", "menu1.TERMINAL_TYPE");
    map.put("type", "menu1.TYPE");
    map.put("orderLevel", "menu1.ORDER_LEVEL");
    map.put("name", "menu1.NAME");
    map.put("id", "menu1.ID");

    return map;
  }


}
